﻿using System;
using System.Xml;
using cz.uhul.DSMLv2;
using System.Configuration;

namespace cz.uhul.DSMLv2.Server.Sql
{



  public static partial class LdRequestServerExtension
  {





    public static LdResponseDocument ProcessBatch(LdRequestDocument rd)
    {
      LdResponseDocument response = new LdResponseDocument();

      //AccessControlTypeEnum acl = AccessControlTypeEnum.none;

      ////read from configuration
      //string authMode = ConfigurationManager.AppSettings["LdAuthMode"]; //none, required

      //if (string.IsNullOrEmpty(authMode))
      //  authMode = "required";

      //else if(authMode.ToLower() != "none")
      //  authMode = "required";

      //if (authMode.ToLower() == "none")
      //{
      //  //prepare to unlimited access to LD
      //  if (rd.AuthRequest != null)
      //    response.AuthResponse = new LdAuthResponse(LdResultCodeEnum.AuthMethodNotSupported, null, null);

      //  //assign full set of acl rights
      //  acl = AccessControlTypeEnum.add | AccessControlTypeEnum.read | AccessControlTypeEnum.modify | AccessControlTypeEnum.delete;
      //}
      //else
      //{
      //  ///request.RequestId
        
      //  if (rd.AuthRequest == null)
      //  {
      //    _fillResponseAsErr(rd, response, LdResultCodeEnum.InappropriateAuthentication, "not authorized", "error: authRequest missing");
      //    return response;
      //  }
      //  else
      //  {
      //    //handle auth
      //    LdAuthResponse auth = (LdAuthResponse)rd.AuthRequest.Execute();

      //    response.AuthResponse = (LdAuthResponse)rd.AuthRequest.Execute();
      //    if (auth.ResultCode.Code != LdResultCodeEnum.Success)
      //    {
      //      //todo:resolve all other requests as error LdResultCodeEnum.StrongAuthRequired
      //      _fillResponseAsErr(rd, response, LdResultCodeEnum.InappropriateAuthentication, "not authorized", "permission denied for LD");
      //      return response;
      //    }

      //    try
      //    {
      //      //when authorized resolve bitmask
      //      acl = (AccessControlTypeEnum)int.Parse(auth.ResultCode.Description.Split(':')[1]);
      //    }
      //    catch (Exception e)
      //    {
      //      _fillResponseAsErr(rd, response, LdResultCodeEnum.InappropriateAuthentication, "not authorized", e.Message);
      //      return response;
      //    }

      //  }
      //}

      //&& (acl & AccessControlTypeEnum.add) != AccessControlTypeEnum.none

      //handle add req
      if (rd.AddRequests != null )
        foreach (LdAddRequest r in rd.AddRequests)
          response.AddResponses.Add((LdAddResponse)r.Execute());

      if (rd.ModifyRequests != null)
        foreach (LdModifyRequest r in rd.ModifyRequests)
          response.ModifyResponses.Add((LdModifyResponse)r.Execute());

      if (rd.SearchRequests != null)
        foreach (LdSearchRequest r in rd.SearchRequests)
          response.SearchResponses.Add((LdSearchResponse)r.Execute());

      if (rd.CompareRequests != null)
        foreach (LdCompareRequest r in rd.CompareRequests)
          response.CompareResponses.Add((LdCompareResponse)r.Execute());

      if (rd.DeleteRequests != null)
        foreach (LdDeleteRequest r in rd.DeleteRequests)
          response.DeleteResponses.Add((LdDeleteResponse)r.Execute());

      if (rd.ModifyDnRequests != null)
        foreach (LdModifyDnRequest r in rd.ModifyDnRequests)
          response.ModifyDnResponses.Add((LdModifyDnResponse)r.Execute());

      if (rd.ExtendedRequests != null)
        foreach (LdExtendedRequest r in rd.ExtendedRequests)
          response.ExtendedResponses.Add((ExtendedResponse)r.Execute());

      return response;

    }





    /// <summary>
    /// when auth error, user cant process any request from batch.
    /// thus all requests in batch must return error
    /// </summary>
    /// <param name="rd"></param>
    /// <param name="response"></param>
    /// <param name="code"></param>
    /// <param name="description">description of problem</param>
    /// <param name="err">error message propagated from original error message</param>
    private static void _fillResponseAsErr(LdRequestDocument rd, LdResponseDocument response, LdResultCodeEnum code, string description, string err)
    {
      //handle add req
      if (rd.ModifyRequests != null)
        foreach (LdModifyRequest r in rd.ModifyRequests)
          response.ModifyResponses.Add(new LdModifyResponse(code, description, err));

      if (rd.SearchRequests != null)
        foreach (LdSearchRequest r in rd.SearchRequests)
          response.SearchResponses.Add(new LdSearchResponse(code, description, err));

      if (rd.AddRequests != null)
        foreach (LdAddRequest r in rd.AddRequests)
          response.AddResponses.Add(new LdAddResponse(code, description, err));

      if (rd.DeleteRequests != null)
        foreach (LdDeleteRequest r in rd.DeleteRequests)
          response.DeleteResponses.Add(new LdDeleteResponse(code, description, err));

      if (rd.ModifyDnRequests != null)
        foreach (LdModifyDnRequest r in rd.ModifyDnRequests)
          response.ModifyDnResponses.Add(new LdModifyDnResponse(code, description, err));

      if (rd.CompareRequests != null)
        foreach (LdCompareRequest r in rd.CompareRequests)
          response.CompareResponses.Add(new LdCompareResponse(code, description, err));

      if (rd.ExtendedRequests != null)
        foreach (LdExtendedRequest r in rd.ExtendedRequests)
          response.ExtendedResponses.Add(new ExtendedResponse(code, description, err));

    }




  }

}